<?php
/**
 * TOP的客户端模型
 * 
 * @version $Id: TopSender.php 467 2012-10-27 03:25:56Z linsir123 $
 */
class TopSender
{
	/**
	 * @var Key
	 */
	public $appkey;
	
	/**
	 * @var Secret
	 */
	public $secretKey;
	
	/**
	 * @var SESSION
	 */
	public $sesionKey;
	
	/**
	 * @var 客户端实例
	 */
	public $client;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		$conf = App::O('taobaoke.app');
		///
		list($this->appkey, $this->secretKey) = $conf;
	}
	
	/**
	 * 设置Sesionkey
	 */
	public function login($sesionKey)
	{
		$this->sesionKey = $sesionKey;
	}
	
	/**
	 * 执行请求
	 */
	public function execute($request, $format = 'json')
	{
		$client = $this->_getClient();
		$client->format = $format;
		///
		return $client->execute($request, $this->sesionKey);
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 请求客户端
	 */
	private function _getClient()
	{
		if (empty($this->client)) {
			if ( ! class_exists('TopClient')) 
				App::V('taobao');
				
			///
			$this->client = App::singleton('TopClient');
			$this->client->secretKey = $this->secretKey;
			$this->client->appkey = $this->appkey;
		}
		
		return $this->client;
	}
}
